package com.wzy.springcloud.service.impl;

import com.wzy.springcloud.dao.OrderDao;
import com.wzy.springcloud.domain.Order;
import com.wzy.springcloud.service.AccountService;
import com.wzy.springcloud.service.OrderService;
import com.wzy.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info(">>>>>>>>开始创建订单");
        orderDao.create(order);

        log.info(">>>>>>>>订单微服务开始调用库存,做扣减");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info(">>>>>>>>订单微服务开始调用库存,做扣减,完成");

        log.info(">>>>>>>>订单微服务开始调用账户,做账户金额的扣减");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info(">>>>>>>>订单微服务开始调用账户,做账户金额的扣减,完成");

        //修改订单的状态
        log.info(">>>>>>>>修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info(">>>>>>>>修改订单状态结束");

        log.info(">>>>>>>>订单流程结束O(∩_∩)O");
    }
}
